package com.sfh.base.ui.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.base.R;


/**
 * 功能描述:
 *
 * @author SunFeihu 孙飞虎
 * @date 2016/11/15
 */

public interface AppDialog {
    /***
     * 提示类对话框
     */
    class ToastDialog extends AlertDialog {
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvLeftClick;
        private TextView tvRightClick;
        private View vLine;
        private IAlertTemple temple;
        private int gravity;

        protected ToastDialog(Context context) {
            super(context, R.style.base_common_toast_dialog);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.base_app_widget_toast_dialog);
            this.tvTitle = (TextView) findViewById(R.id.tvTitle);
            this.tvContent = (TextView) findViewById(R.id.tvContent);
            this.tvLeftClick = (TextView) findViewById(R.id.tvLeftClick);
            this.tvRightClick = (TextView) findViewById(R.id.tvRightClick);
            this.vLine =  findViewById(R.id.vLine);
            //
            // 默认处理点击
            this.tvLeftClick.setOnClickListener(mLOnClickListener);
            this.tvRightClick.setOnClickListener(mROnClickListener);

            if ( this.temple == null) {
                return;
            }
            this.tvContent.setMovementMethod (ScrollingMovementMethod.getInstance ());
            this.tvTitle.setText(TextUtils.isEmpty(temple.title()) ? "提示" : temple.title());
            this.tvContent.setText(TextUtils.isEmpty(temple.message()) ? "" : temple.message());
            this.tvLeftClick.setText(TextUtils.isEmpty(temple.leftText()) ? "取消" : temple.leftText());
            this.tvRightClick.setText(TextUtils.isEmpty(temple.rightText()) ? "确定" : temple.rightText());

            this.tvLeftClick.setVisibility(temple.isLeftVisibility()? View.VISIBLE: View.GONE);
            this.vLine.setVisibility(temple.isLeftVisibility()? View.VISIBLE: View.GONE);

        }

        public void setTemple(IAlertTemple temple) {
            this.temple = temple;
        }

        public void setGravity(int gravity){
            this.gravity  =gravity;
        }
        // 默认事件
        private View.OnClickListener mLOnClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (temple == null) {
                    dismiss();
                    return;
                }
                // 上层使用覆盖默认事件
                final OnClickListener lClick = temple.onLeftClick();
                if (lClick != null) {
                    lClick.onClick(ToastDialog.this,v.getId());
                }
            }
        };

        // 默认事件
        private View.OnClickListener mROnClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (temple == null) {
                    dismiss();
                    return;
                }
                // 上层使用覆盖默认事件
                final OnClickListener lClick = temple.onRightClick();
                if (lClick != null) {
                    lClick.onClick(ToastDialog.this,v.getId());
                }
            }
        };

        @Override
        public void show() {
            Context context = getContext();
            if (context == null) {
                return;
            }
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                if (activity.isFinishing()) {
                    return;
                }
            }
            super.show();
            this.tvContent.setGravity (gravity);
        }

    }


    /***
     * 等待类型对话框
     */
    class ProgressDialog extends AlertDialog {
        private TextView tvTip;
        private CharSequence message;
        private final String DEFAULT_MSG = "请稍候...";
        private LinearLayout ryBG;
        boolean color;

        protected ProgressDialog(Context context) {
            super(context, R.style.base_common_progress_dialog);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.base_app_widget_progress_dialog);

            this.ryBG = (LinearLayout) findViewById(R.id.ryBG);
            this.tvTip = (TextView) findViewById(R.id.tvTip);
            this.message = TextUtils.isEmpty(message) ? DEFAULT_MSG : message;

        }

        public void setBackgroundColor(boolean color ){
            this.color = color;
        }

        @Override
        public void setMessage(CharSequence message) {
            this.message = message;
        }

        @Override
        public void show() {
            Context context = getContext();
            if (context == null) {
                return;
            }
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                if (activity.isFinishing()) {
                    return;
                }
            }
            super.show();
            this.tvTip.setText(message);
            if ( this.color){
                this.ryBG.setBackgroundResource (R.drawable.base_net_loading_bg);
                this.tvTip.setTextColor (Color.WHITE);
            }else{
                this.ryBG.setBackgroundColor (Color.TRANSPARENT);
               // this.tvTip.setTextColor (getContext ().getResources ().getColor (R.color.color_656565));
            }
        }

    }

}
